package com.thkj.framework.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thkj.dao.LoginRecordMapper;
import com.thkj.util.RmLoginUserInfo;
import com.thkj.util.RmUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.thkj.util.Util;

public class LoginInterceptor extends HandlerInterceptorAdapter {
	@Autowired
	private LoginRecordMapper loginRecordDao;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		//判断来自手机端还是PC端的访问
		String requestHeader = request.getHeader("user-agent");
        if(Util.isMobileDevice(requestHeader)){
        	request.setAttribute("isMobile", true);
        }else{
        	request.setAttribute("isMobile", false);
        }
		RmLoginUserInfo loginUserInfo = RmUtil.getLoginUserFromCookie(request);
		//如果cookie没有登录信息则跳转到首页，有登录信息则转换为对象存入到request方便取用
		if(loginUserInfo == null){
			RmUtil.noLogin(request, response);
			return false;
        }else{
        	RmUtil.setLoginUserInfo(request, loginUserInfo);
        	return true;
        }
	}
	
}
